package cn.tworice.blog.service.impl;

import cn.tworice.blog.entity.QRCode;
import cn.tworice.blog.service.QRCodeService;
import cn.tworice.blog.utils.QRCodeUtil;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Service
@Slf4j
public class QRCodeServiceImpl implements QRCodeService {

    @Value("${blog.QRCodeFile}")
    private String filePath;

    /**
     * 保存图片到本地
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @Override
    public String saveFile(MultipartFile file) {
        // 如果file是空
        if(file==null){
            return null;
        }

        String newName=System.currentTimeMillis()+".jpg";
        try {
            file.transferTo(new File(filePath+newName));
        }catch (Exception ex){
            log.error("保存二维码文件出现异常"+ex.getMessage());
        }

        return filePath+newName;
    }

    /**
     * 生成二维码
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @Override
    public String createCode(QRCode qrCode, String logoPath) throws Exception{
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        if(qrCode.getColor()!=null){
            log.info("设置Color-"+qrCode.getColor());
            qrCodeUtil.setColor(qrCode.getColor());
        }
        if(qrCode.getBgc()!=null){
            log.info("设置Background-"+qrCode.getBgc());
            qrCodeUtil.setBgc(qrCode.getBgc());
        }
        if(qrCode.getMargin()!=null){
            log.info("设置Margin-"+qrCode.getMargin());
            qrCodeUtil.setMARGIN(qrCode.getMargin());
        }
        if(qrCode.getLevel()!=null){
            log.info("设置纠错等级-"+qrCode.getLevel());
            qrCodeUtil.setLevel(qrCode.getLevel());
        }
        if(qrCode.getSize()!=null){
            log.info("设置二维码尺寸-"+qrCode.getLevel());
            qrCodeUtil.setQRCODE_SIZE(qrCode.getSize());
        }

        String path = qrCodeUtil.encode(qrCode.getContent(), logoPath, this.filePath,qrCode.getIsCompress());
        return path;
    }

    /**
     * 将MultipartFile对象转为File对象，并解析二维码内容
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @Override
    public String parseCode(MultipartFile logo) throws Exception{
        File file=new File(logo.getOriginalFilename());
        FileUtils.copyInputStreamToFile(logo.getInputStream(),file);

        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        String result = qrCodeUtil.decode(file);

        if (file.exists()) {
            file.delete();
        }
        return result;
    }

    /**
     * 清理生成的二维码文件
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @Override
    public RequestResult clearQRCodeFiles() {
        RequestResult result = new RequestResult();
        try {
            File file = new File(this.filePath);
            if(file.isDirectory()){
                File[] files = file.listFiles();
                for(File f:files){
                    f.delete();
                }
            }
            result.setCode(new StatusCode(200,"清理成功"));
        }catch (Exception ex){
            ex.printStackTrace();
            result.setCode(new StatusCode(500,"清理异常"));
        }
        return result;
    }
}
